import 'package:casdon/core/utils/string_externtion.dart';

extension StringExtensions on String {
  /// 将字符串转换为首字母大写
  String get capitalize {
    if (isEmpty) return this;
    return this[0].toUpperCase() + substring(1).toLowerCase();
  }

  /// 移除字符串中的所有空格
  String get removeAllWhitespace {
    return replaceAll(RegExp(r'\s+'), '');
  }

  /// 判断字符串是否为数字
  bool get isNumeric {
    return double.tryParse(this) != null;
  }

  String get cropParams100 {
    if (isImagePath() == false) {
      return this;
    }
    if (contains('oss.casdon.com')) {
      return "$this?imageView2/1/w/200/h/200/format/webp/interlace/1/ignore-error/1";
    }

    if (contains('oss-dev.casdon.com') || contains('oss-cloud.casdon.com')) {
      return "$this?x-oss-process=image/auto-orient,1/resize,w_100,h_100,m_lfit,q_100";
    }

    if (contains('oss-uat.casdon.com')) {
      return "$this?x-oss-process=image/auto-orient,1/resize,w_300,h_300,m_lfit,q_100";
    }

    return this;
  }

  String get cropParams200 {
    if (isImagePath() == false) {
      return this;
    }
    if (contains('oss.casdon.com')) {
      return "$this?imageView2/1/w/200/h/200/format/webp/interlace/1/ignore-error/1";
    }

    if (contains('oss-dev.casdon.com') ||
        contains('oss-cloud.casdon.com') ||
        contains('oss-uat.casdon.com')) {
      return "$this?x-oss-process=image/auto-orient,1/resize,w_300,h_300,m_lfit,q_100";
    }
    return this;
  }

  String get cropParams300 {
    if (isImagePath() == false) {
      return this;
    }
    if (contains('oss.casdon.com')) {
      return "$this?imageView2/1/w/300/h/300/format/webp/interlace/1/ignore-error/1";
    }

    if (contains('oss-dev.casdon.com') ||
        contains('oss-cloud.casdon.com') ||
        contains('oss-uat.casdon.com')) {
      return "$this?x-oss-process=image/auto-orient,1/resize,w_300,h_300,m_lfit,q_100";
    }

    return this;
  }

  String get cropParams500 {
    if (isImagePath() == false) {
      return this;
    }
    if (contains('oss.casdon.com')) {
      return "$this?imageView2/1/w/500/h/500/format/webp/interlace/1/ignore-error/1";
    }

    if (contains('oss-dev.casdon.com') ||
        contains('oss-cloud.casdon.com') ||
        contains('oss-uat.casdon.com')) {
      return "$this?x-oss-process=image/auto-orient,1/resize,w_500,h_500,m_lfit,q_100";
    }
    return this;
  }

  String get cropParams700 {
    if (isImagePath() == false) {
      return this;
    }
    if (contains('oss.casdon.com')) {
      return "$this?imageView2/1/w/700/h/700/format/webp/interlace/1/ignore-error/1";
    }

    if (contains('oss-dev.casdon.com') ||
        contains('oss-cloud.casdon.com') ||
        contains('oss-uat.casdon.com')) {
      return "$this?x-oss-process=image/auto-orient,1/resize,w_700,h_700,m_lfit,q_100";
    }

    return this;
  }

  String get cropParams1000 {
    if (isImagePath() == false) {
      return this;
    }
    if (contains('oss.casdon.com')) {
      return "$this?imageView2/1/w/1000/h/1000/format/webp/interlace/1/ignore-error/1";
    }

    if (contains('oss-dev.casdon.com') ||
        contains('oss-cloud.casdon.com') ||
        contains('oss-uat.casdon.com')) {
      return "$this?x-oss-process=image/auto-orient,1/resize,w_1000,h_1000,m_lfit,q_100";
    }

    return this;
  }

  String get cropParams1300 {
    if (isImagePath() == false) {
      return this;
    }
    if (contains('oss.casdon.com')) {
      return "$this?imageView2/1/w/1000/h/1000/format/webp/interlace/1/ignore-error/1";
    }

    if (contains('oss-dev.casdon.com') ||
        contains('oss-cloud.casdon.com') ||
        contains('oss-uat.casdon.com')) {
      return "$this!m";
    }

    return this;
  }

  String get cropParamsForSE {
    if (isImagePath() == false) {
      return this;
    }
    if (contains('oss.casdon.com')) {
      return "$this?imageView2/1/w/1125/h/2001/format/webp/interlace/1/ignore-error/1";
    }

    if (contains('oss-dev.casdon.com') ||
        contains('oss-cloud.casdon.com') ||
        contains('oss-uat.casdon.com')) {
      return "$this?x-oss-process=image/auto-orient,1/resize,w_1125,h_2001,m_lfit,q_100";
    }

    return this;
  }

  String strippingHTML() {
    // 第一步：去除图片标签（包括 figure 和 img）
    final imagePattern = r'<figure[^>]*>.*?<img[^>]*>.*?</figure>|<img[^>]*>';
    var cleanedText = replaceAll(
      RegExp(imagePattern, multiLine: true, caseSensitive: false, dotAll: true),
      '',
    );

    // 第二步：去除所有HTML标签
    final htmlTagPattern = r'<[^>]+>';
    cleanedText = cleanedText.replaceAll(RegExp(htmlTagPattern), '');

    // 第三步：替换HTML实体（包括 &nbsp; &amp; 等）
    final htmlEntityPattern = r'&[a-z0-9]+;|&#[0-9]+;';
    cleanedText = cleanedText.replaceAll(
      RegExp(htmlEntityPattern, caseSensitive: false),
      ' ',
    );

    // 第四步：规范化空格和换行
    cleanedText = cleanedText.replaceAll(RegExp(r'\s+'), ' ').trim();

    return cleanedText;
  }
}
